print("Hello, 拉取抖音博主数据")


# pip install DrissionPage

# 先配置环境 将chrome.exe设置到默认浏览器中
# from DrissionPage import ChromiumOptions
# path = r'C:\Program Files\Google\Chrome\Application\chrome.exe' # 自己电脑内chrom浏览器exe所在位置
# ChromiumOptions().set_browser_path(path).save()

from datetime import datetime

# pip install pymysql
import pymysql

headers = {
    'referer': 'https://www.douyin.com/',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36'
}

def save_up_to_mysql(name, uid, author_main_page_url, avatar_image):
    try:
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', charset='utf8mb4', db='video')
        cursor = conn.cursor()

        add_time = datetime.now()

        # 插入数据的SQL语句
        insert_query = "INSERT INTO author_info (up_uid, up_name, up_header_image, up_main_page_url, create_time) VALUES (%s, %s, %s, %s, %s)"
        cursor.execute(insert_query, (uid, name, avatar_image, author_main_page_url, add_time))

        # 提交事务
        conn.commit()
        print(f'博主 {name} 的信息已保存到数据库')

    except conn.Error as err:
        print(f"异常 插入数据时出错: {err}")

    finally:
        # 关闭游标和数据库连接
        if cursor:
            cursor.close()
        if conn:
            conn.close()

def author_exists(author_main_page_url):
    """
    检查作者是否已经存在于数据库中
    :param author_main_page_url: 主页URL
    :return: 如果存在返回True，否则返回False
    """
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', charset='utf8mb4', db='video')
    cursor = conn.cursor()
    sql = "SELECT * FROM author_info WHERE up_main_page_url = %s"
    cursor.execute(sql, [author_main_page_url])
    result = cursor.fetchone()
    cursor.close()
    conn.close()
    return result is not None

try:
    from DrissionPage import Chromium
    from DrissionPage.common import Keys

    tab = Chromium().latest_tab

    tab.get('https://www.douyin.com/user/self?from_tab_name=main&showTab=post')

    ele = tab.ele('.cIss_G7b')
    ele.click()

    while True:

        # 监听数据包 请求路径是：https://www.douyin.com/aweme/v1/web/user/following/list/?xxx
        tab.listen.start('/aweme/v1/web/user/following/list/')

        resp = tab.listen.wait(timeout=2)

        if not resp:
            print("没有收到数据包，可能是页面加载慢，重试中...")

            script = """
                   ele = document.getElementsByClassName('eq0kzn5a')[0];
                   if (ele) {
                       ele.style.overflow = 'auto';  // 确保目标元素可滚动
                       ele.scrollBy(0, 400);         // 向下滚动 400 像素
                   } else {
                       console.error('未找到目标元素');
                   }
                   """
            tab.run_js(script)
            continue
        json_data = resp.response.body
        # print(json_data)

        following_list = json_data['followings']
        print(f'当前页码 {len(following_list)} 条数据')
        if not following_list:
            print('没有更多数据了，退出循环')
            break

        for index in following_list:
            name = index['nickname']
            uid = index['uid']
            author_main_page_id = index['sec_uid']
            avatar_image = index['avatar_thumb']['url_list'][0]

            author_main_page_url = "https://www.douyin.com/user/" + author_main_page_id

            print(f'博主昵称: {name}, 用户主页地址: {author_main_page_url} 头像: {avatar_image}')

            # 根据video_id去查mysql库，如果已经存在，则跳过
            # 这里是查询数据库的操作
            if author_exists(author_main_page_url):
                print(f'{name} - {author_main_page_url} 已经存在')
                continue
            else:
                # 保存入mysql库
                print(f'{name} - {author_main_page_url} 开始保存到数据库')
                # 这里可以添加保存到数据库的逻辑
                save_up_to_mysql(name, uid, author_main_page_url, avatar_image)

        script = """
        ele = document.getElementsByClassName('eq0kzn5a')[0];
        if (ele) {
            ele.style.overflow = 'auto';  // 确保目标元素可滚动
            ele.scrollBy(0, 400);         // 向下滚动 400 像素
        } else {
            console.error('未找到目标元素');
        }
        """
        tab.run_js(script)

except Exception  as e:
    print(f'发生异常: {e}')
finally:
    # 确保浏览器关闭
    # dp.quit()
    print("异常处理，浏览器已关闭。")










